/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.bomt.domain.vo;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.comac.ins.bomt.domain.DetailStrengthStandardChange;
import com.comac.ins.bomt.domain.vo.writehandler.StrengthStandardChangeDetailWriteHandler;
import com.comac.ins.bomt.utils.BomVersionUtil;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * The type Detail strength standard change vo.
 *
 * @author zhanghongyi
 * @date 2024 /8/5 16:15
 * @description DetailStrengthStandardChange
 */
@Data
@EqualsAndHashCode(callSuper = true)
@AutoMapper(target = DetailStrengthStandardChange.class)
@SuperBuilder
@NoArgsConstructor
public class DetailStrengthStandardChangeVo extends DetailStrengthBomVo {
    /**
     * 变更类型
     */
    private String changeType;

    /**
     * 导出强度数模清单Vs标准装机清单变更明细
     *
     * @param outputStream the output stream
     * @param sheetName    the sheet name
     * @param data         the data
     */
    public static void exportStrengthStandardChange(OutputStream outputStream, String sheetName, List<DetailStrengthStandardChangeVo> data) {
        //准备head
        List<String> heads = Lists.newArrayList();
        heads.addAll(Arrays.asList("层级", "图号", "名称"));
        List<String> versions = BomVersionUtil.getVersions(data);
        heads.addAll(versions);
        //准备数据
        List<List<Object>> toBeExportData = Lists.newArrayList();
        for (DetailStrengthBomVo vo : data) {
            List<Object> row = Lists.newArrayList();
            row.add(vo.getLevel());
            row.add(vo.getCode());
            row.add(vo.getName());
            for (String version : versions) {
                if (StringUtils.equals(version, vo.getVersion())) {
                    row.add(1);
                } else {
                    row.add(null);
                }
            }
            toBeExportData.add(row);
        }
        //进行导出
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteCellStyle.setWriteFont(headWriteFont);

        try (ExcelWriter writer = EasyExcelFactory.write(outputStream).autoCloseStream(false).registerWriteHandler(new StrengthStandardChangeDetailWriteHandler(data)).registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, (WriteCellStyle) null)).build()) {
            List<List<String>> headTitles = Lists.newArrayList();
            for (String head : heads) {
                headTitles.add(List.of(head));
            }
            WriteSheet incomeSheet = new WriteSheet();
            incomeSheet.setHead(headTitles);
            incomeSheet.setSheetName(sheetName);

            writer.write(toBeExportData, incomeSheet);
            writer.finish();
        }
    }
}
